﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Net;

namespace Pavilion.Communications
{
    public class DesignerClient : BaseClient
    {
        private static DesignerClient instance;
        static public DesignerClient Instance { get { return (instance == null) ? new DesignerClient() : instance; } }

        public IDesignerService Proxy { get { return (IDesignerService)proxy; } }

        public DesignerClient()
        {
            instance = this;

            base.Initialize(7000);
        }

        protected override ICommunicationObject CreateProxy(NetTcpBinding tcpBinding, EndpointAddress endpointAddress)
        {
            return (ICommunicationObject)ChannelFactory<IDesignerService>.CreateChannel(tcpBinding, endpointAddress);
        }

        public void TransferData(Action<IDesignerService> action)
        {
            if (ConnectionStatus != ConnectionStatus.Open)
                return;

            try { action.Invoke(Proxy); }
            catch { }
        }

        public void TransferDataThreaded(Action<IDesignerService> action)
        {
            System.Threading.Tasks.Task.Factory.StartNew(() => TransferData(action));
        }
    }
}
